package com.ssm.controller.student;

import com.ssm.controller.BaseController;
import com.ssm.entity.Departmentpeople;
import com.ssm.entity.DepartmentpeopleExample;
import com.ssm.service.DepartmentPeopleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class StudentHomeController extends BaseController {
    @Resource(name = "departmentPeopleService")
    private DepartmentPeopleService departmentPeopleService;
    //转到后台管理-主页
    @RequestMapping(value = "student/home",method = RequestMethod.GET)
    public String goToPage(HttpSession session, Map<String,Object> map){
        logger.info("检查学生权限");
        String studentId=checkAdmin(session);
        if (!studentId.equals("studentId")){
            return "redirect:/";
        }
        Object o=session.getAttribute("studentId");
        logger.info("显示id"+o);
        DepartmentpeopleExample departmentpeopleExample=new DepartmentpeopleExample();
        //根据student id 查询departmentPeople表
         departmentpeopleExample.or().andIdEqualTo(Integer.parseInt(o.toString()));
        List<Departmentpeople> d =departmentPeopleService.getDepartmentPeopleExple(departmentpeopleExample);
        //logger.info("----------->"+d.get(0));
        int peoplestatus=1;
        if (d.isEmpty()){
            peoplestatus=0;
        }
        map.put("peoplestatus",peoplestatus);
        return "student/homePage";

    }
    @RequestMapping(value = "student/account/logout",method = RequestMethod.GET)
    public String logout(HttpSession session) {
        logger.info("检查管理员权限");
        String adminId=checkAdmin(session);
        if (adminId.equals("studentId")){
            session.removeAttribute("studentId");
            session.invalidate();
            logger.info("登录信息已清除，返回登录页");
            return "redirect:/";
        }
        return "redirect:/";
    }

}
